#include "scene.h"
#include"barrier.h"
#include"coin.h"
#include"role.h"
#include<QGraphicsPixmapItem>
#include<QRandomGenerator>

QList<int> randY = {120, 220, 320, 420};

Scene::Scene(QObject *parent) : QGraphicsScene(parent){
    //设置大小并加载背景图片
    setSceneRect(0,0,720,620);
    Background=new background;
    addItem(Background);
    //添加4条轨道
    auto i1 = new QGraphicsPixmapItem(QPixmap(":/res/track.png"));
    i1->setScale(100/i1->pixmap().height());
    i1->setPos(0,100);
    auto i2 = new QGraphicsPixmapItem(QPixmap(":/res/track.png"));
    i2->setScale(100/i1->pixmap().height());
    i2->setPos(0,200);
    auto i3 = new QGraphicsPixmapItem(QPixmap(":/res/track.png"));
    i3->setScale(100/i1->pixmap().height());
    i3->setPos(0,300);
    auto i4 = new QGraphicsPixmapItem(QPixmap(":/res/track.png"));
    i4->setScale(100/i1->pixmap().height());
    i4->setPos(0,400);
    addItem(i1);
    addItem(i2);
    addItem(i3);
    addItem(i4);
    QPixmap roleimage(":/res/role1.png");
    Role=new role(roleimage);
    addItem(Role);

    score = 0;

    scoretext = new QGraphicsTextItem();
    scoretext->setFont(QFont("Arial", 20, QFont::Bold));
    scoretext->setDefaultTextColor(QColor("#ffffff"));
    scoretext->setPos(10, 10);
    scoretext->setPlainText(QString("Score: ") + QString::number(score));
    addItem(scoretext);

    connect(this,&Scene::collidewithcoin,this,&Scene::collidecoin);

    timer=new QTimer(this);
    connect(timer,&QTimer::timeout,this,[=](){run();});

    timer1 = new QTimer(this);
    timer2 = new QTimer(this);
    timer1->start(600-score*5);
    timer2->start(650-score*7);
    connect(timer1, &QTimer::timeout, this, &Scene::addrandomcoins);
    connect(timer2, &QTimer::timeout, this, &Scene::addrandombarriers);


}

void Scene::addrandombarriers(){
    QPixmap barrierimage(":/res/barrier.png");
    barrierimage = barrierimage.scaled(50,50);
    qreal barrier_x=QRandomGenerator::global()->bounded(630,660);
    qreal barrier_y=random_barrier_y();
    barrier* Barrier=new barrier(barrierimage,barrier_x,barrier_y);
    addItem(Barrier);

}


qreal Scene::random_barrier_y(){
    int i = QRandomGenerator::global()->bounded(4);
    return randY[i];
}

void Scene::addrandomcoins(){
    QPixmap coinimage(":/res/coin.png");
    coinimage = coinimage.scaled(50,50);
    //qreal coin_x=640;
    qreal coin_x=QRandomGenerator::global()->bounded(630,660);
    qreal coin_y=random_coin_y();
    coin* Coin=new coin(coinimage,coin_x,coin_y);
    addItem(Coin);

}

void Scene::run()
{

     showscore();

    //碰撞检测，需要检测是否同一行
        QList<QGraphicsItem*> collidingItems = Role->collidingItems();
        int roleY = (Role->pos().y() - 10 ) / 100;
        foreach (QGraphicsItem * item , collidingItems){
            auto *c= dynamic_cast<coin*>(item);
            auto *ba = dynamic_cast<barrier*>(item);
            int dy = randY.indexOf(item->pos().y());
            if(c && roleY == dy){
                removeItem(c);
                delete c;
                emit collidewithcoin();
            }
            else
            {
                //auto ba = dynamic_cast<barrier*>(item);
                if(ba && roleY == dy )
                {
                    emit Gameover(score);
                    qDebug()<<"game over"<<roleY<<dy;
                }
            }

        }
}

void Scene::start()
{
    timer->start(100);
}

void Scene::stop()
{
    timer->stop();
}


void Scene::keyPressEvent(QKeyEvent *keyEvent)
{
    Role->KeyPressEvent(keyEvent);
}


qreal Scene::random_coin_y(){

    int i = QRandomGenerator::global()->bounded(4);
    return randY[i];
}



void Scene::collidecoin(){
    score++;
    showscore();

}

void Scene::showscore(){
    scoretext->setPlainText(QString("Score: ") + QString::number(score));
}
